<template>
  <BasicModal ref="basicModal" v-bind="$attrs" @register="registerModal" destroyOnClose :width="1000" @ok="handleSubmit" @cancel="closeCallback">
    <template #title>
      <div style="display: flex; justify-content: space-between; align-items: center">
        {{ title }}
        <a-button style="margin-right: 100px" type="primary" @click="previewClick">{{ showAsset ? '关闭预览' : '预览待盘点资产' }}</a-button></div
      >
    </template>
    <BasicForm ref="basicForm" @register="registerForm" />
    <BasicTable v-show="showAsset" @register="registerTable">
      <!-- <template #headerTop><button @click="selectAsset">选择资产</button></template> -->
    </BasicTable>
  </BasicModal>
</template>

<script lang="ts" setup>
  import { ref, computed, unref } from 'vue';
  import { BasicModal, useModalInner } from '/@/components/Modal';
  import { BasicForm, useForm } from '/@/components/Form/index';
  import { formSchema } from '../InventoryPlan.data';
  import { saveOrUpdate } from '../InventoryPlan.api';
  import { useI18n } from '/@/hooks/web/useI18n';
  import { BasicTable } from '/@/components/Table/index';
  import { useListPage } from '/@/hooks/system/useListPage';
  import { assetColumns } from '../InventoryPlan.data';
  import { list } from '/@/views/asset/asset-info/AssetInfo.api';
  import { watchEffect } from 'vue';
  const { t } = useI18n('common');
  // Emits声明
  const emit = defineEmits(['register', 'success']);
  const isUpdate = ref(true);
  const basicForm = ref<InstanceType<typeof BasicForm>>();
  const showAsset = ref(false);
  const basicModal = ref<InstanceType<typeof BasicModal>>();
  const previewClick = () => {
    showAsset.value = !showAsset.value;
  };
  //列表配置
  let formParams: any = {};

  const { tableContext } = useListPage({
    // designScope: 'basic-table-demo-ajax',
    tableProps: {
      maxHeight: 250,
      title: '当前条件待盘点资产',
      useSearchForm: false,
      api: list,
      columns: assetColumns,
      showActionColumn: false,
      beforeFetch: (params) => {
        console.log('$$$$ params', params);
        return Object.assign(params, formParams);
      },
      rowSelection: { type: 'checkbox' },
    },
  });
  const [registerTable, { reload }, { rowSelection, selectedRowKeys }] = tableContext;

  let stopWatcher: any = null;
  //表单配置
  const [registerForm, { setProps, resetFields, setFieldsValue, validate }] = useForm({
    // labelWidth: 200
    schemas: formSchema,
    showActionButtonGroup: false,
    baseColProps: { span: 12 },
  });
  //产品包信息
  // const inventoryPackData = reactive<any>({});
  //表单赋值
  const [registerModal, { setModalProps, closeModal }] = useModalInner(async (data) => {
    //重置表单
    await resetFields();
    setModalProps({ confirmLoading: false, showCancelBtn: !!data?.showFooter, showOkBtn: !!data?.showFooter, height: 600 });
    isUpdate.value = !!data?.isUpdate;
    if (unref(isUpdate)) {
      //表单赋值
      await setFieldsValue({
        ...data.record,
      });
      // Object.assign(inventoryPackData, data.record);
      await reload();
    }
    stopWatcher = watchEffect(() => {
      let params: any = {};
      params.cartegory = basicForm.value?.formModel.categoryName;
      params.position = basicForm.value?.formModel.positionName;
      params.useState = basicForm.value?.formModel.useState;
      console.log('change');
      if (JSON.stringify(params) !== JSON.stringify(formParams)) {
        formParams = params;
        reload();
      }
    });
    // 隐藏底部时禁用整个表单
    setProps({ disabled: !data?.showFooter });
  });
  function closeCallback() {
    stopWatcher();
  }
  //设置标题
  const title = computed(() => (!unref(isUpdate) ? t('addText') : t('editText')));
  //表单提交事件
  async function handleSubmit(_v) {
    try {
      let values = await validate();
      console.log(values);
      values.checkState = '1';
      values.inventoryState = '1';
      setModalProps({ confirmLoading: true });
      //提交表单
      values.selectedRowKeys = selectedRowKeys.value;
      await saveOrUpdate(values, isUpdate.value);
      //关闭弹窗
      closeModal();
      //刷新列表
      emit('success');
    } finally {
      setModalProps({ confirmLoading: false });
    }
  }
  defineExpose({ basicForm, basicModal });
  //选择资产
  // const selectAssetList: Ref<Recordable<any>[]> = ref([]);
  // function selectAsset() {
  // console.log(rowSelection, '11111');
  // console.log(selectedRows, '22222');
  // console.log(selectedRowKeys, '43333');
  // }
</script>

<style lang="less" scoped>
  /** 时间和数字输入框样式 */
  :deep(.ant-input-number) {
    width: 100%;
  }

  :deep(.ant-calendar-picker) {
    width: 100%;
  }
</style>
